#!/usr/bin/bash

# set -x
set -e

if [[ $# -ne 1 ]]; then
    echo "Usage $0 [container ID/name]" >&2
    exit 1
fi

CONTAINER_NAME="$1"
BASE_DIR="/var/lib/ks-scmc/containers"

SOCK_DIR=${BASE_DIR}/${CONTAINER_NAME}/socket
AUTH_FILE=${BASE_DIR}/${CONTAINER_NAME}/xauth/Xauthority

HOSTNAME=$(docker inspect -f '{{ .Config.Hostname }}' ${CONTAINER_NAME})

# Get the DISPLAY slot
DISPLAY_NUMBER=$(echo $DISPLAY | cut -d. -f1 | cut -d: -f2)

# X11 auth permission
AUTH_COOKIE=$(xauth list | grep "^$(hostname)/unix:${DISPLAY_NUMBER} " | awk '{print $3}')
if [[ -f "${AUTH_FILE}" ]]; then
    > ${AUTH_FILE}
fi
xauth -f ${AUTH_FILE} add ${HOSTNAME}/unix:0 MIT-MAGIC-COOKIE-1 ${AUTH_COOKIE}

# Proxy with the :0 DISPLAY
socat UNIX-LISTEN:${SOCK_DIR}/X0,fork TCP4:localhost:60${DISPLAY_NUMBER} &

# make sure container is running
IS_RUNNING=$(docker inspect -f '{{ .State.Running }}' ${CONTAINER_NAME})
if [[ "${IS_RUNNING}" == "false" ]]; then
    docker start ${CONTAINER_NAME}
fi

docker exec -it ${CONTAINER_NAME} /bin/bash
